/**
 * Created by liujie on 2017/10/4.
 */
(function ($,window,undefined) {
    var $container = $("div#divContent");
    $.extend({
        /**
         * 阻止事件冒泡
         * @param evt
         */
        stopEventBubbe:function (evt) {
            var ev = evt ? evt : window.event;
            if(ev && ev.stopPropagation){
                ev.stopPropagation();
            }else {
                ev.cancelBubble = true;
            }
        },
        /**
         * 页面跳转
         * @param url
         * @param params
         * @param link
         */
        loadPage:function (url,params,link) {
           if(url.indexOf("?") !=-1){
               for(var k in params){
                   url = url +"&"+k+"="+params[k];
               }
           }else{
               var init_index = 0;
               for(var k in params){
                    if(!init_index){
                        url =url+"?"+k+"="+params[k];
                    }else{
                        url = url +"&"+k+"="+params[k];
                    }
                   init_index++;
               }
           }
           $container.load(url)
        },
        link:function (url) {
            window.location.href = url
        },
        open:function (url) {
            window.open(url)
        }
    });
    $.ArrayUtils = {
        find:function (arr,callBack) {
            if(arr &&arr.length){
                for(var i = 0,_len = arr.length;i<_len;i++){
                    var r= callBack(arr[i]);
                    if(r){
                        return true
                    }
                }
            }
            return false;
        },
        findIndex:function (arr,callBack) {
            if(arr &&arr.length){
                for(var i = 0,_len = arr.length;i<_len;i++){
                    var r= callBack(arr[i]);
                    if(r){
                        return parseInt(i)
                    }
                }
            }
            return parseInt(-1);
        },
        unique:function (arr) {
            var res = [];
            var json = {};
            for(var i = 0; i < arr.length; i++){
                if(!json[arr[i]]){
                    res.push(arr[i]);
                    json[arr[i]] = 1;
                }
            }
            return res;

        }
    }
})(jQuery,window,undefined);

;(function($,window,undefined){
    $.loadingBar = {
        $body:$($("body")[0]),
        _barId:"#loading-bar-id",
        _roundId:"#loader-id",
        _bgId:"#loading-bar-bg-id",
        _loadedMessageId:"#loadedValue",
        dom:{
            _bg_dom:'<div class="loading-bar-bg" id="loading-bar-bg-id"></div>',
            _bar_dom:'<div class="loading-bar-box" id="loading-bar-id">' +
            '<div class="loading-bar-sport">' +
            '</div>' +
            '<span class="loading-bar-box-span">' +
            ' <em id="roundValue">20</em>%' +
            '</span>' +
            '</div>',
            _round_dom:'<div class="loader-box" id="loader-id">' +
            '<div class="tools-msg" id="loadedValue">加载中...</div>' +
            '<div class="loader-box-content">' +
            '</div>' +
            '</div>'
        },
        _createDom:function(msg,time,flag){
            if(this._isHasDom($(this._bgId))){
                this._removeDom($(this._bgId));
            }
            this.$body.append(this.dom._bg_dom);
            //进度条
            if(flag === "bar"){
                if(this._isHasDom($(this._barId))){
                    this._removeDom($(this._barId));
                }
                this.$body.append(this.dom._bar_dom);
            }else{
                if(this._isHasDom($(this._roundId))){
                    this._removeDom($(this._roundId));
                }
                this.$body.append(this.dom._round_dom);
                $(this._loadedMessageId).text(msg ? msg : "加载中...");
                if(time){
                    setTimeout(function(){
                        var _the = this;
                        return function () {
                            _the.hide();
                        }
                    }.call(this),parseInt(time))
                }
            }

        },
        _removeDom:function($dom){
            $dom.remove();
        },
        _isHasDom:function($dom){
            return $dom && $dom.length;
        },
        _bindDomEvent:function(){

        },
        show:function(msg,time,flag){
            this._createDom(msg,time,flag)
        },
        hide:function(){
            if(this._isHasDom($(this._bgId))){
                this._removeDom($(this._bgId));
            }
            if(this._isHasDom($(this._barId))){
                this._removeDom($(this._barId));
            }
            if(this._isHasDom($(this._roundId))){
                this._removeDom($(this._roundId));
            }
        }
    }
})(jQuery,window,undefined);